﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Data.SqlClient;

using EHTSaleManager.Code;
using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Utility;
using EHTSaleManager.Code.Entity;

namespace EHTSaleManager
{
    public partial class IndexListProduct : System.Web.UI.Page
    {
        private Int32 categoryID = -1 ;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 10;
        private Int32 totalRecord;       

        protected void Page_Load(object sender, EventArgs e)
        {
            
            try
            {
                categoryID = Int32.Parse(Request.QueryString["CategoryID"].ToString());
                lbCategoryname.Text = CategoryProvider.GetCategoryByCategoryID(categoryID).CategoryName;
            }
            catch (Exception ex)
            {
                Form.Action = "Index.aspx";
            }

            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch (Exception ex)
            {
                pageIndex = 1;
            }

            pageSize = this.PaggingControl1.PageSize;

            if (!Page.IsPostBack)
            {
                dtlProductList.DataSource = ProductProvider.GetProductsByCategoryID(categoryID, pageIndex, pageSize, out totalRecord);
                dtlProductList.DataBind();

                this.PaggingControl1.PageURL = "IndexListProduct.aspx?CategoryID="+categoryID;
                this.PaggingControl1.TotalRecord = totalRecord;
                this.PaggingControl1.PagingSize = pageSize;
                
            }
        }
       
        protected void ImgBuy_Click(object sender, ImageClickEventArgs e)
        {                        
            ImageButton btn = (ImageButton)sender;
            string ProductID = btn.CommandArgument;
            try
            {               
                for (int i = 0; i < dtlProductList.Items.Count; i++)
                {  
                   TextBox proID = (TextBox)(dtlProductList.Items[i].FindControl("txtProID"));
                    if (proID.Text == ProductID.ToString())
                   {
                     TextBox txtquatity = (TextBox)(dtlProductList.Items[i].FindControl("txtquantity"));

                    int quantity = Int32.Parse(txtquatity.Text.ToString());
                    if (quantity == 0)
                    {
                        ltrmessage.Text = "Vennligst skriv inn mengde";
                        Response.Redirect("IndexListProduct.aspx?CategoryID="+categoryID);
                    }
                    else
                    {
                        ShoppingCart.Instance.AddItem(Int32.Parse(ProductID.ToString()), quantity);
                        Response.Redirect("IndexListProduct.aspx?CategoryID=" + categoryID);
                    }
                   }
                }                        
               
            }
            catch (Exception ex)
            {
                ltrmessage.Text = "Erorr: Antall er ikke numberic";
            }
                                  
        }
       
        
    }
}